package com.example.haohao.myview;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.media.Image;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.RequiresApi;


/**
 * @author fjwu :
 * @discription : 分时最后一个闪烁点
 * create on 2020/2/24
 */

public class HeartBeat extends View {

    private float scale = 1;
    private Paint linePaint;
    private int r = DensityUtils.dp2px(6);
    private ValueAnimator scaleAnim;

    private PointF pointF;
    private int mMeasureWidth;
    private int mWidthMode;
    private int mMeasureHeight;
    private int mHeightMode;


    private PointF startPointF;
    private PointF endPointF;
    private PointF controlPointF1, controlPointF2;

    private PointF startPointF2;
    private PointF endPointF2;
    private PointF controlPointF3, controlPointF4;

    int radius;

    public HeartBeat(Context context) {
        super(context);
        init();
    }

    public HeartBeat(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public HeartBeat(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    /**
     * 测量
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidthMode = MeasureSpec.getMode(widthMeasureSpec);
        mHeightMode = MeasureSpec.getMode(heightMeasureSpec);
        mMeasureWidth = MeasureSpec.getSize(widthMeasureSpec);
        mMeasureHeight = MeasureSpec.getSize(heightMeasureSpec);
        if (mWidthMode == MeasureSpec.AT_MOST && mHeightMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(200, 200);
        } else if (mWidthMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(200, mMeasureHeight);
        } else if (mHeightMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(mMeasureWidth, 200);
        } else {
            setMeasuredDimension(mMeasureWidth, mMeasureHeight);
        }
    }


    /**
     * 初始化
     */
    private void init(){
        pointF=new PointF();

        pointF.x=100;
        pointF.y=100;
        linePaint = new Paint();
//        linePaint.setColor(Color.parseColor("#979797"));
        linePaint.setColor(Color.RED);
        linePaint.setStrokeWidth(3.0f);
        linePaint.setAntiAlias(true);
        if(scaleAnim == null) {
            scaleAnim = ValueAnimator.ofFloat(1, 0.5f, 1);
            scaleAnim.setDuration(2000);
            scaleAnim.setRepeatCount(-1);
            scaleAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    scale = (float) animation.getAnimatedValue();
                        postInvalidate();
                }
            });
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.BLACK);
//        canvas.drawColor(Color.BLACK);
//        linePaint.setColor(Color.parseColor("#8838A4FF"));
//        if(pointF != null ) {
//                linePaint.setColor(Color.parseColor("#8838A4FF"));
//                canvas.drawCircle(pointF.x, pointF.y, r * scale, linePaint);
//                linePaint.setColor(Color.parseColor("#38A4FF"));
//                canvas.drawCircle(pointF.x, pointF.y, r/2, linePaint);
//        }

//        int width = getWidth();//获取屏幕宽
//        int height = getHeight();//获取屏幕高

//        /**
//         *  绘制心形
//         */
//        //左半面
//        linePaint.setColor(Color.RED);
//        Path path = new Path();
//        path.moveTo(width / 2, height / 4);
//        path.cubicTo((width * 6) / 7, height / 9, (width * 12) / 13, (height * 2) / 5, width / 2, (height * 7) / 12);
//        canvas.drawPath(path, linePaint);
//        //右半面
//        linePaint.setColor(Color.YELLOW);
//        Path path2 = new Path();
//        path2.moveTo(width / 2, height / 4);
//        path2.cubicTo(width / 7, height / 9, width / 13, (height * 2) / 5, width / 2, (height * 7) / 12);
//        canvas.drawPath(path2, linePaint);
//
//        //左半面
//        linePaint.setColor(Color.BLUE);
//        Path path3 = new Path();
//        path3.moveTo(width / 2, height / 4*3/2);
//        path3.cubicTo((width * 6) / 7/2, height / 9*2, (width * 12) / 13, (height * 2) / 5/2, width / 2, (height * 7) / 12*9/10);
//        canvas.drawPath(path3, linePaint);
//        //右半面
//        linePaint.setColor(Color.WHITE);
//        Path path4 = new Path();
//        path4.moveTo(width / 2, height / 4*3/2);
//        path4.cubicTo(width / 7*4, height / 9*2, width / 13, (height * 2) / 5/2, width / 2, (height * 7) / 12*9/10);
//        canvas.drawPath(path4, linePaint);


/*      ********************************************************************       */

        int x = getWidth()/2;//获取屏幕宽

        int y = getHeight()/2;//获取屏幕高
        radius = radius==getWidth()/8?getWidth()/4:getWidth()/8;//获取屏幕宽
        double d = (2 * Math.sqrt(2) - 1);
        this.startPointF = new PointF(x, y - radius);
        this.endPointF = new PointF(x, y + radius / 10);
        this.controlPointF1 = new PointF(x, (float) (y - d * radius));
        this.controlPointF2 = new PointF((float) (x + d * radius), (float) (y - d * radius));

        this.startPointF2 = new PointF(x, y - radius);
        this.endPointF2 = new PointF(x, y + radius / 10);
        this.controlPointF3 = new PointF(x, (float) (y - d * radius));
        this.controlPointF4 = new PointF((float) (x - d * radius), (float) (y - d * radius));

        this.linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        this.linePaint.setColor(Color.WHITE);

        //绘制贝塞尔曲线
        Path path = new Path();

        path.moveTo(startPointF.x, startPointF.y);
        path.cubicTo(controlPointF1.x, controlPointF1.y, controlPointF2.x, controlPointF2.y, endPointF.x, endPointF.y);
        canvas.drawPath(path, linePaint);

        path.moveTo(startPointF2.x, startPointF2.y);
        path.cubicTo(controlPointF3.x, controlPointF3.y, controlPointF4.x, controlPointF4.y, endPointF2.x, endPointF2.y);
        canvas.drawPath(path, linePaint);
    }

    /**
     * 开始动画
     */
    private void startDrawAni(){
        if(scaleAnim != null){
            if(!scaleAnim.isStarted()){
                scaleAnim.start();
            }
        }
    }


    /**
     * 结束动画
     */
    private void stopDrawAni(){
        if(scaleAnim != null){
            if(scaleAnim.isStarted()){
                scaleAnim.end();
            }
        }
    }


    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        startDrawAni();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        stopDrawAni();
    }


}
